#!/bin/bash

# NVIM software management script
# This script provides functions for managing Neovim installation

# Default configuration
NVIM_VERSION=${nvim_version:-"v0.11.4"}
NVIM_NAME="nvim-linux-x86_64"
NVIM_ENV_VAR="NVIM_HOME"
NVIM_DOWNLOAD_PATH="/tmp/nvim-download"
PROXY_PREFIX=${proxy_prefix:-"https://ghproxy.net/"}

# Get software information
get_software_info() {
    cat << EOF
Software Name: Neovim
Description: Hyperextensible Vim-based text editor
Version: ${NVIM_VERSION}
Homepage: https://neovim.io/
Installation Path: ${softwares_path}/${NVIM_NAME}
Environment Variable: ${NVIM_ENV_VAR}
EOF
}

# Download and install Neovim
execute_install_software() {
    local softwares_path="$1"
    local nvim_home="${softwares_path}/${NVIM_NAME}"
    
    echo "Starting Neovim installation..."
    echo "Version: ${NVIM_VERSION}"
    echo "Installation path: ${nvim_home}"
    
    # Create download directory
    mkdir -p "${NVIM_DOWNLOAD_PATH}"
    
    # Download Neovim
    local download_url="${PROXY_PREFIX}https://github.com/neovim/neovim/releases/download/${NVIM_VERSION}/${NVIM_NAME}.tar.gz"
    echo "Downloading Neovim ${NVIM_VERSION}..."
    echo "Download URL: $download_url"
    curl -C - -o "${NVIM_DOWNLOAD_PATH}/${NVIM_NAME}.tar.gz" -LO "$download_url"
    
    if [[ $? -ne 0 ]]; then
        echo "Error: Failed to download Neovim"
        return 1
    fi
    
    # Create software directory
    sudo mkdir -p "${softwares_path}"
    
    # Remove existing installation if it exists
    if [[ -d "${nvim_home}" ]]; then
        echo "Removing existing installation..."
        sudo rm -rf "${nvim_home}"
    fi
    
    # Extract and install
    echo "Installing Neovim to ${nvim_home}..."
    sudo tar -C "${softwares_path}" -xzf "${NVIM_DOWNLOAD_PATH}/${NVIM_NAME}.tar.gz"
    
    if [[ $? -eq 0 ]]; then
        echo "Neovim installation completed successfully"
        return 0
    else
        echo "Error: Failed to install Neovim"
        return 1
    fi
}

# Set environment configuration
set_rc_config() {
    local rc_path="$1"
    local nvim_home="${softwares_path}/${NVIM_NAME}"
    
    cat << EOF

# ${NVIM_ENV_VAR} configuration
${NVIM_ENV_VAR}="${nvim_home}"
export PATH="\${PATH}:\${${NVIM_ENV_VAR}}/bin"
EOF
}

# Uninstall Neovim
execute_uninstall_software() {
    local softwares_path="$1"
    local nvim_home="${softwares_path}/${NVIM_NAME}"
    
    echo "Starting Neovim uninstallation..."
    
    # Remove installation directory
    if [[ -d "${nvim_home}" ]]; then
        echo "Removing Neovim installation directory: ${nvim_home}"
        sudo rm -rf "${nvim_home}"
    else
        echo "Neovim installation directory not found: ${nvim_home}"
    fi
    
    # Remove download cache
    if [[ -d "${NVIM_DOWNLOAD_PATH}" ]]; then
        echo "Cleaning download cache: ${NVIM_DOWNLOAD_PATH}"
        rm -rf "${NVIM_DOWNLOAD_PATH}"
    fi
    
    echo "Neovim uninstallation completed"
    return 0
}

# Check if Neovim is installed
check_installed() {
    local softwares_path="$1"
    local nvim_home="${softwares_path}/${NVIM_NAME}"
    
    if [[ -d "${nvim_home}" ]] && [[ -x "${nvim_home}/bin/nvim" ]]; then
        echo "Neovim is installed at: ${nvim_home}"
        echo "Version: $("${nvim_home}/bin/nvim" --version | head -n1)"
        return 0
    else
        echo "Neovim is not installed"
        return 1
    fi
}

# Main function for direct script execution
main() {
    if [[ "$1" == "info" ]]; then
        get_software_info
    elif [[ "$1" == "install" ]]; then
        execute_install_software "${softwares_path:-/opt/softwares}"
    elif [[ "$1" == "uninstall" ]]; then
        execute_uninstall_software "${softwares_path:-/opt/softwares}"
    elif [[ "$1" == "check" ]]; then
        check_installed "${softwares_path:-/opt/softwares}"
    else
        echo "Usage: $0 {info|install|uninstall|check}"
        exit 1
    fi
}

# If script is executed directly, run main function
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi